﻿//-----------------------------------------------------------------------
// <copyright file="SetSceneSelectionAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action
{
    using System;
    using TileTool.Action;
    using TileTool.Log;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Action to set the selection in the scene.
    /// </summary>
    public class SetSceneSelectionAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "SetSceneSelectionAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// New selection for the scene.
        /// </summary>
        private IEntityCollection newSelection;

        /// <summary>
        /// Old scene selection.
        /// </summary>
        private IEntityCollection oldSelection;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="scene">Scene the action is operating on.</param>
        /// <param name="selection">New selection for the scene.</param>
        public SetSceneSelectionAction(ISceneManager scene, IEntityCollection selection) 
            : base(scene)
        {
            if (scene == null)
            {
                throw new ArgumentNullException("scene", "Scene must be non-null.");
            }

            if (selection == null)
            {
                throw new ArgumentNullException("selection", "Selection must be non-null.");
            }

            this.newSelection = selection;
            this.oldSelection = scene.Selection.Clone();
            this.Valid = true;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Do() - Setting selection to " + this.newSelection.Count + " items.");

            this.ValidateEntities(this.newSelection);

            Scene.SetSelection(this.newSelection);
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Undo() - Setting selection to " + this.oldSelection.Count + " items.");

            this.ValidateEntities(this.oldSelection);

            Scene.SetSelection(this.oldSelection);
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Validate the entities to make sure they are in the correct scene.
        /// </summary>
        /// <param name="collection">Collection of entities to validate.</param>
        private void ValidateEntities(IEntityCollection collection)
        {
            foreach (IEntity entity in collection)
            {
                if (entity.Scene != this.Scene)
                {
                    throw new InvalidOperationException("Entity scene does not match scene for action.");
                }
            }
        }

        #endregion
    }
}
